    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SpringLayout;
import javax.swing.text.DefaultCaret;

public class DescriptionBlock extends JLayeredPane {
    private JEditorPane text_area;
    private JLabel type_label;
    private SpringLayout layout;
    private TitlePhrase title;
    private boolean is_active;
    
    public DescriptionBlock(Description d) {
        layout = new SpringLayout();
        setLayout(layout);
        
        setActive(false);
        
        draw();
    }
    
    public void draw() {
        title = new TitlePhrase("");

        type_label = new JLabel();
        type_label.setForeground(Color.WHITE);
        type_label.setFont(new Font("Comic Sans MS", Font.BOLD, 13));
        
        text_area = new JEditorPane();
        //set the caret to never update, such that the scrollbar is always on top initially
        DefaultCaret caret = new DefaultCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        text_area.setCaret(caret);
        
        text_area.setOpaque(false);
        text_area.setFocusable(false);
        text_area.setEditable(false);
        text_area.setContentType("text/html");
        
        redraw();
    }
    
    private void redraw() {
        removeAll();
        
        text_area.setMaximumSize(new Dimension(0, 0));
        
        add(title);
        String[] invisible_types = {"Name", "Tags", "General"};
        if(!Arrays.asList(invisible_types).contains(type_label.getText()))
            add(type_label);
        if(!text_area.getText().equals(""))
            add(text_area);
        
        layout.putConstraint(SpringLayout.WEST, title, 0, SpringLayout.WEST, this);

        layout.putConstraint(SpringLayout.WEST, type_label, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, type_label, 0, SpringLayout.NORTH, this);

        layout.putConstraint(SpringLayout.EAST, text_area, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, text_area, 10, SpringLayout.WEST, this);
        if(!Arrays.asList(invisible_types).contains(type_label.getText()))
            layout.putConstraint(SpringLayout.NORTH, text_area, 0, SpringLayout.SOUTH, type_label);
        else
            layout.putConstraint(SpringLayout.NORTH, text_area, 0, SpringLayout.NORTH, this);
        
        if(title.isEmpty())
            layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, text_area);
        else
            layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, title);
    }
    
    public void setText(LinkedList<String> description, HashSet<String> filter) {
        String main_text = "", text = "";
        
        String head = description.remove();
        type_label.setText(head);
        
        if(head.equals("Name"))
            title = new TitlePhrase(description.getFirst(), "medium");
        else {
            title = new TitlePhrase("");
            for(String s : description) {
                String line = s.replace("(newline)", "<br>");
                line = markFilterStrings("text", line, filter);
                text += line + "<br><span style='font-size:10%'><br></span>";
            }
            if(text.length() > 43) {
            	main_text = "<span style='font-family:Comic Sans MS;font-size:85%;color:white'>" + text.substring(0, text.length() - 43) + "</span>";
            } else {
            	main_text = "";
            }
        }
        
        text_area.setText(main_text);
        redraw();
    }
    
    private String markFilterStrings(String type, String text, HashSet<String> filter) {
        int replace_id = 0;
        HashMap<String, String> replace_map = new HashMap<String, String>();
        for(String s : filter) {
            for(String ind_s : s.split("[+]")) {
                if(ind_s.startsWith(type + ":") && !ind_s.substring(type.length() + 1).equals("")) {
                    HashSet<String> set = new HashSet<String>();
                    Pattern p = Pattern.compile("(?i)" + ind_s.substring(type.length() + 1));
                    Matcher m = p.matcher(text);
                    while(m.find())
                        set.add(m.group());
                    for(String replace : set) {
                        text = text.replace(replace, "####" + replace_id + "####");
                        replace_map.put("####" + replace_id + "####", "<span style='background-color:red'>" + replace + "</span>");
                        replace_id++;
                    }
                }
            }
        }
        for(String key : replace_map.keySet()) {
            text = text.replace(key, replace_map.get(key));
        }
        
        return text;
    }
    
    public void setActive(boolean active) {
        is_active = active;
        setVisible(active);
    }
    
    public boolean isActive() {
        return is_active;
    }
}
